import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import tests.sign_in
from common.utils import forum_driver


class IndexTest:
    url = ""
    driver = ""

    def __init__(self):
        self.driver = forum_driver.driver
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver.implicitly_wait(5)

    def test_access_index_without_login(self):
        # 保存当前url
        prev_url = self.driver.current_url
        print(f"访问网址: {self.url}")
        self.driver.get(self.url)
        # 未登录直接访问会跳转到sign_in页面
        if "sign-in" in prev_url or "sign-up" in prev_url:
            assert "sign-in" in self.driver.current_url, "未登录直接访问了主页"

    # 切换板块
    def switch_section(self):
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(2) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(3) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(4) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(5) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(6) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(7) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(8) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(9) > a > span.nav-link-title").click()
        self.driver.find_element(By.CSS_SELECTOR, "#topBoardList > li:nth-child(10) > a > span.nav-link-title").click()
        forum_driver.get_screenshot()
        self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a > span.nav-link-title").click()

    def read_post(self):
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong").click()
        forum_driver.get_screenshot()
        # 发送私信
        print("发送私信")
        self.driver.find_element(By.CSS_SELECTOR, "#btn_details_send_message").click()
        wait = WebDriverWait(self.driver, 10)
        input_elem = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#index_message_receive_content")))
        input_elem.send_keys("你好")
        self.driver.find_element(By.CSS_SELECTOR, "#btn_index_send_message").click()
        forum_driver.get_screenshot()
        # 点赞
        print("点赞")
        self.driver.find_element(By.CSS_SELECTOR, "#details_btn_like_count").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right "
                                                  "> div > span.close-jq-toast-single")  # 成功
        forum_driver.get_screenshot()
        # 空内容发布评论
        print("空内容发布帖子评论")
        btn = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#details_btn_article_reply")))
        # 利用JavaScript的scrollIntoView方法,让页面滚动,使得传入的元素btn滚动到可见区域
        self.driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", btn)
        time.sleep(4)  # 等待页面滚动动画和点赞提示消失
        btn.click()
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div").text
        assert "请输入回复内容" in text
        forum_driver.get_screenshot()
        # 有内容发布评论
        print("有内容发布帖子评论")
        js_code = """
        var editor = document.querySelector('.CodeMirror').CodeMirror;
        editor.setValue('Orz');
        """
        self.driver.execute_script(js_code)
        time.sleep(1)  # 等待输入完成
        self.driver.find_element(By.CSS_SELECTOR, "#details_btn_article_reply").click()
        # 查找最新一条评论
        wait = WebDriverWait(self.driver, 10)
        wait.until(EC.presence_of_all_elements_located(
            (By.XPATH, "//*[starts-with(@id, 'details_article_reply_content_')]/p")))  # 确认元素已经渲染
        comment = self.driver.find_element(By.XPATH,
                                           "(//*[starts-with(@id, 'details_article_reply_content_')]/p)[1]").text
        assert comment == "Orz"
        forum_driver.get_screenshot()

    def add_post(self):
        print("发布帖子")
        # 回到首页
        #本该用滚动+点击首页，这里取巧刷新
        self.driver.refresh()
        #点击+发布帖子前往发帖页面
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        #未输入标题发布
        wait = WebDriverWait(self.driver, 10)
        submit = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#article_post_submit")))
        self.driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", submit)
        #等待滚动动画消失
        time.sleep(1)
        submit.click()
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div").text
        assert "请输入帖子标题" in text
        #未输入内容发布
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").send_keys("发帖")
        js_code = """
                var editor = document.querySelector('.CodeMirror').CodeMirror;
                editor.setValue('发帖内容');
                """
        self.driver.execute_script(js_code)
        time.sleep(1)  # 等待输入完成
        submit = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#article_post_submit")))
        self.driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", submit)
        # 等待滚动动画消失
        time.sleep(1)
        submit.click()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div")

    def del_post(self):
        print("删除帖子")
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        time.sleep(0.2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_profile").click()
        self.driver.find_element(By.CSS_SELECTOR, "#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate > a > strong").click()
        self.driver.find_element(By.CSS_SELECTOR, "#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a").click()
        self.driver.find_element(By.CSS_SELECTOR, "#details_artile_delete").click()


# tests.sign_in.SignInTest().signin_suc_test()
# index = IndexTest()
# # index.test_access_index_without_login()
# # index.switch_section()
# # index.read_post()
# index.del_post()
